home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / chrome / content / sbIAsyncForLoop.js < prev    next >
Text File  |  2006-02-07  |  5KB  |  187 lines

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. //
  28. // sbIAsyncForLoop wrapper
  29. //
  30.  
  31. try
  32. {
  33. // Global setup
  34. var sbIAsyncForLoopCount = 0;
  35. var sbIAsyncForLoopArray = {};
  36.  
  37. function sbIAsyncForLoop( aInitEval, aWhileEval, aStepEval, aBodyEval, aFinishedEval, aStepsPerInterval, aIntervalDelay )
  38. {
  39.   if ( ( aStepsPerInterval == null ) || ( aStepsPerInterval == 0 ) )
  40.   {
  41.     aStepsPerInterval = 1;
  42.   }
  43.   if ( aIntervalDelay == null )
  44.   {
  45.     aIntervalDelay = 0;
  46.   }
  47.   try
  48.   {
  49.     // Assign ourselves to the global space so we can have a proper "this" pointer.
  50.     this.m_Index         = sbIAsyncForLoopCount++;
  51.     sbIAsyncForLoopArray[ this.m_Index ] = this;
  52.  
  53.     // Set the eval strings variables
  54.     this.m_InitEval      = aInitEval;
  55.     this.m_WhileEval     = aWhileEval;
  56.     this.m_StepEval      = aStepEval;
  57.     this.m_BodyEval      = aBodyEval;
  58.     this.m_FinishedEval  = aFinishedEval;
  59.     this.m_StepsPer      = aStepsPerInterval;
  60.     this.m_Delay         = aIntervalDelay;
  61.     
  62.     this.cancel = function()
  63.     {
  64.       clearInterval( this.m_Interval );
  65.       this.m_Interval = null;
  66.     }
  67.     
  68.     this.step = function( index )
  69.     {
  70.       if ( this.m_Interval )
  71.       {
  72.         for ( var StepsPerCount = 0; StepsPerCount < this.m_StepsPer; StepsPerCount++ )
  73.         {
  74.           var pop = false;
  75.           var step = false
  76.           try
  77.           {
  78.             if ( typeof( this.m_WhileEval ) == 'function' )
  79.               step = this.m_WhileEval();
  80.             else          
  81.               step = eval( this.m_WhileEval );
  82.           }
  83.           catch ( err )
  84.           {
  85.             alert( "sbIAsyncForLoop::eval( this.m_WhileEval )\r\n" + this.m_WhileEval + "\r\n" + err );
  86.           }
  87.           if ( step )
  88.           {
  89.             try
  90.             {
  91.               if ( typeof( this.m_BodyEval ) == 'function' )
  92.                 pop = this.m_BodyEval() == true;
  93.               else          
  94.                 pop = eval( this.m_BodyEval ) == true;
  95.             }
  96.             catch ( err )
  97.             {
  98.               alert( "sbIAsyncForLoop::eval( this.m_BodyEval )\r\n" + this.m_BodyEval + "\r\n" + err );
  99.             }
  100.             try
  101.             {
  102.               if ( typeof( this.m_StepEval ) == 'function' )
  103.                 this.m_StepEval();
  104.               else          
  105.                 eval( this.m_StepEval );
  106.             }
  107.             catch ( err )
  108.             {
  109.               alert( "sbIAsyncForLoop::eval( this.m_StepEval )\r\n" + this.m_StepEval + "\r\n" + err );
  110.             }
  111.             // Optionally pop out of the m_StepsPer loop.
  112.             if ( pop )
  113.             {
  114.               break;
  115.             }
  116.           }
  117.           else
  118.           {
  119.             try
  120.             {
  121.               if ( typeof( this.m_FinishedEval ) == 'function' )
  122.                 this.m_FinishedEval();
  123.               else          
  124.                 eval( this.m_FinishedEval );
  125.             }
  126.             catch ( err )
  127.             {
  128.               alert( "sbIAsyncForLoop::eval( this.m_FinishedEval )\r\n" + this.m_FinishedEval + "\r\n" + err );
  129.             }
  130.             clearInterval( this.m_Interval );
  131.             break;
  132.           }
  133.         }
  134.       }
  135.     }
  136.     
  137.     // And start things off
  138.     try
  139.     {
  140.       if ( typeof( this.m_InitEval ) == 'function' )
  141.         this.m_InitEval();
  142.       else          
  143.         eval( this.m_InitEval );
  144.     }
  145.     catch ( err )
  146.     {
  147.       alert( "sbIAsyncForLoop::eval( this.m_InitEval )\r\n" + this.m_InitEval + "\r\n" + err );
  148.     }
  149.     this.m_Interval = setInterval( "sbIAsyncForLoopArray[ " + this.m_Index + " ].step()", this.m_Delay );
  150.   }
  151.   catch ( err )
  152.   {
  153.     alert( "sbIAsyncForLoop - construct\r\n" + err );
  154.   }
  155. }
  156.  
  157. /*
  158.  
  159. // Sample Code
  160.  
  161. function f()
  162. {
  163.   this.i = 0;
  164. }
  165. var loop = new sbIAsyncForLoop
  166.   // aInitEval, aWhileEval, aStepEval, aBodyEval, aFinishedEval
  167.   f, // "this.i = 0",
  168.   "this.i <= 10",
  169.   "this.i++",
  170.   function()
  171.   {
  172.     alert( this.i );
  173.   },
  174.   "alert( this.text );"
  175. );
  176. // loop.i = 0;
  177. loop.text = "'Finished!'"
  178.  
  179. */
  180.  
  181. }
  182. catch ( err )
  183. {
  184.   alert( "sbIAsyncForLoop - load\r\n" + err );
  185. }
  186.